package com.spring.boot.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @program: springboot_vue
 * @description: spring代理quartz对象
 * @author: ry
 * @create: 2018-08-16 14:16
 **/
@Component
public class SpringContext implements ApplicationContextAware {
   protected static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context=applicationContext;
    }
    public static ApplicationContext getContext(){
        return context;
    }

    /**
     * 通过name获取Bean
     * @param name
     * @return
     */
    public static Object getBean(String name){
        return getContext().getBean(name);
    }

    /**
     * 通过class获取Bean
     * @param Clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> Clazz){
        return getContext().getBean(Clazz);
    }

    /**
     * 通过name，以及class返回对应的Bean
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name,Class<T> clazz){
        return getContext().getBean(name, clazz);
    }
}
